<% admin_breadcrumb(link_to plural_resource_name(Spree::LegacyUser), spree.admin_users_path) %>
<% admin_breadcrumb(link_to @user.email, edit_admin_user_url(@user)) %>
<% admin_breadcrumb(Spree.t('admin.user.items_purchased')) %>


<%= render 'spree/admin/users/sidebar' %>
<%= render 'spree/admin/users/tabs', current: :items %>
<%= render :partial => 'spree/admin/users/user_page_actions' %>

<fieldset data-hook="admin_user_items_purchased" class="col-xs-9">
  <legend><%= Spree.t(:"admin.user.items_purchased") %></legend>

  <%= paginate @orders, theme: "solidus_admin" %>

  <% if @orders.any? %>
    <%# TODO add search interface %>
    <table class="stock-contents index" id="listing_items" data-hook="stock-contents">
      <colgroup>
        <col style="width: 10%;" />
        <col style="width: 10%;" />
        <col style="width: 30%;" />
        <col style="width: 8%;" />
        <col style="width: 8%;" />
        <col style="width: 10%;" />
        <col style="width: 14%;" />
      </colgroup>

      <thead>
        <th><%= sort_link @search, :completed_at, Spree::Order.human_attribute_name(:completed_at), {}, {:title => 'orders_completed_at_title'} %></th>
        <th colspan=2><%= Spree::LineItem.human_attribute_name(:description) %></th>
        <th><%= Spree::LineItem.human_attribute_name(:price) %></th>
        <th><%= Spree::LineItem.human_attribute_name(:quantity) %></th>
        <th><%= Spree::Order.human_attribute_name(:total) %></th>
        <th><%= sort_link @search, :state, Spree::Order.human_attribute_name(:state), {}, {:title => 'orders_state_title'} %></th>
        <th><%= sort_link @search, :number, Spree.t(:order_num, :scope => 'admin.user'), {}, {:title => 'orders_number_title'} %></th>
      </thead>

      <tbody>
        <% @orders.each do |order| %>
          <% order.line_items.each do |item| %>
            <tr class="stock-item" data-item-quantity="<%= item.quantity %>">
              <td class="align-center order-completed-at"><%= l(order.completed_at.to_date) if order.completed_at %></td>
              <td class="item-image">
                <%= image_tag item.variant.display_image.attachment(:mini) %>
              </td>
              <td class="item-name">
                <%= item.product.name %><br><%= "(" + variant_options(item.variant) + ")" unless item.variant.option_values.empty? %>
                <% if item.variant.sku.present? %>
                  <strong><%= Spree.t(:sku) %>:</strong> <%= item.variant.sku %>
                <% end %>
              </td>
              <td class="align-center item-price"><%= item.single_money.to_html %></td>
              <td class="align-center item-quantity"><%= item.quantity %></td>
              <td class="align-center item-total"><%= item.money.to_html %></td>
              <td class="align-center order-state">
                <div class="state <%= order.state.downcase %>"><%= Spree.t("order_state.#{order.state.downcase}") %></div>
                <% if order.payment_state %>
                  <div class="state <%= order.payment_state %>"><%= link_to Spree.t("payment_states.#{order.payment_state}"), admin_order_payments_path(order) %></div>
                <% end %>
                <% if Spree::Order.checkout_step_names.include?(:delivery) && order.shipment_state %>
                  <div class="state <%= order.shipment_state %>"><%= Spree.t("shipment_states.#{order.shipment_state}") %></div>
                <% end %>
              </td>
              <td class="align-center order-number">
                <% if can?([:admin, :edit], order) %>
                  <%= link_to order.number, edit_admin_order_url(order) %>
                <% else %>
                  <%= order.number %>
                <% end %>
              </td>
            </tr>
          <% end %>
        <% end %>
    </table>
  <% else %>
    <div class="col-xs-9 no-objects-found">
      <%= render 'spree/admin/shared/no_objects_found',
                   resource: Spree::Order,
                   new_resource_url: spree.new_admin_order_path %>
    </div>
  <% end %>
  <%= paginate @orders, theme: "solidus_admin" %>
</fieldset>
